define(["../common/confirm"], function (Confirm) {

        function Live(options) {
            this.init(options);
        }

        Live.prototype = {
            init: function (options) {
                this.options = options;
                this.confirm = new Confirm({host: this.options.host});
            },
            setOptions: function (options) {
                this.options = $.extend(this.options, options);
            },
            show: function () {
                $("div[data-sign=content]").hide();
                $("#content_live_list").show();
                // 初始化UI组件
                App.initUniform();
                this.initEvents();
                this.refreshTable();
            },
            initEvents: function () {
                var that = this;
                // 搜索that.show();
                $("#btn_search").off("click.search").on("click.search", function () {
                    that.initTable();
                });
                //详情返回按钮
                $("#btn_live_back").off("click.btn_live_back").on("click.btn_live_back", function () {
                    that.show()
                });
                //详情
                $("#table_live_list tbody").off("click.op_video_live_details").on("click.op_video_live_details", "a[data-sign=op_video_live_details]", function () {
                    var $op_video = $(this).closest("span[data-sign=op_video]");
                    that.liveDetails({
                        id: $op_video.data("id")
                    });
                })
                // 调度
                $("#table_live_list tbody").off("click.op_video_dispatch").on("click.op_video_dispatch", "a[data-sign=op_video_dispatch]", function () {
                    var $op_video = $(this).closest("span[data-sign=op_video]");
                    $("#dispatch").attr("data-id", $op_video.data("id"))
                    //  $("#dispatch").attr("data-name", $op_video.data("name"))
                    $(".task").attr("checked", false);
                    $("#task-0").attr("checked", false);
                    $(".task").attr("disabled", false);
                    $(".note-codable").val("")
                    var dispatchBusinessId = $op_video.data("name") + ""
                    if (dispatchBusinessId.indexOf("1000") >= 0) {
                        $(".task").get(0).checked = true
                        $(".task").get(0).disabled = true;
                    }
                    if (dispatchBusinessId.indexOf("1001") >= 0) {
                        $(".task").get(1).checked = true
                        $(".task").get(1).disabled = true;
                    }
                    if (dispatchBusinessId.indexOf("1002") >= 0) {
                        $(".task").get(2).checked = true
                        $(".task").get(2).disabled = true;
                    }
                    if (dispatchBusinessId.indexOf("1003") >= 0) {
                        $(".task").get(3).checked = true
                        $(".task").get(3).disabled = true;
                    }
                });
                //保存调度
                $("#dispatch").off("click.dispatch").on("click.dispatch", function () {
                    var task = document.getElementsByClassName("task");
                    var str = ""
                    // alert( $(".task:checked").val())
                    for (var i = 0; i < task.length; i++) {
                        if (task[i].checked == true) {
                            //  $(".checkbox").find(".task").is(':checked')
                            str = str + $(".task")[i].value + ","
                        }
                    }
                    str = str + $('.note-codable').val()
                    that.dispatchBusinessId({
                        id: $(this).attr("data-id"),
                        name: str
                    });
                });
                //复选框点击
                $("#task-0").off("click.task-0").on("click.task-0", function () {
                    var task = document.getElementsByClassName("task");
                    var task0 = document.getElementById("task-0");
                    for (var i = 0; i < task.length; i++) {
                        if (task[i].disabled == false)
                            task[i].checked = task0.checked;
                    }
                });
                $(".task").off("click.task").on("click.task", function () {
                    var task = document.getElementsByClassName("task");
                    var task0 = document.getElementById("task-0");
                    var m = true;
                    for (var i = 0; i < task.length; i++) {
                        if (task[i].checked == false) {
                            task0.checked = false;
                            m = false;
                            break;
                        }
                    }
                    if (m) {
                        task0.checked = true;
                    }
                });
            },
            getQueryParams: function () {
                var search_videoTitle = $("#form_live_search input[name=search_videoTitle]").val();
                var search_sourceCountry = $("#selectCountry>option:selected").attr("value")
                var search_videoType = $("#form_live_search input[name=search_videoType]").val();
                return [
                    {name: "search_videoTitle", value: search_videoTitle},
                    {name: "search_sourceCountry", value: search_sourceCountry},
                    {name: "search_videoType", value: search_videoType},
                ];
            },
            initTable: function () {
                var that = this;
                this.dataTable = $("#table_live_list").dataTable({
                        iDisplayLength: 20,
                        bProcessing: true,
                        bServerSide: true,
                        bSort: false,
                        bFilter: false,
                        bAutoWidth: false,
                        bDestroy: true,
                        sDom: "tr<'row'<'col-sm-6'i><'col-sm-6'p>>",
                        sAjaxSource: this.options.host + "/video/list.json",
                        sServerMethod: "POST",
                        aoColumns: [
                            {sTitle: "id", sWidth: "100px", mData: "id"},
                           /* {sTitle: "test", sWidth: "100px", mData: "test"},*/
                            {
                                sTitle: "标题", sWidth: "320px", mData: null,
                                fnRender: function (obj) {
                                    return "<span style='word-break:break-all;white-space:normal; width:100%;'>" + obj.aData.title + "</span>"
                                }
                            },
                            {
                                sTitle: "所属国家", sWidth: "120px", mData: null,
                                fnRender: function (obj) {
                                    return "<span style='word-break:break-all;white-space:normal; width:100%;'>" + obj.aData.sourceCountry + "</span>"
                                }
                            },
                            {sTitle: "发起用户", sWidth: "190px", mData: "nickName"},
                            {sTitle: "发起时间", sWidth: "220px", mData: "str_startTime"},
                            {
                                sTitle: "结束时间", sWidth: "220px", mData: null,
                                fnRender: function (obj) {
                                    switch (obj.aData.ptype) {
                                        case 1:
                                            return '未结束';
                                        case 3:
                                            return obj.aData.endTime;
                                        default:
                                            return '结束';
                                    }
                                }
                            },
                            {
                                sTitle: "播放平台", sWidth: "260px", mData: "str_dispatchBusinessId"
                            },
                            {
                                sTitle: "更多操作", sWidth: "20%", mData: null,
                                fnRender: function (obj) {

                                    var ops = [{
                                        color: "btn-sm btn-warning",
                                        sign: "op_video_dispatch",
                                        defaultModal: "#dispatchModal",
                                        modal: "modal",
                                        id: "",
                                        name: "",
                                        btnName: "调度"
                                    },
                                        {
                                            color: "btn-sm btn-info",
                                            sign: "op_video_live_details",
                                            id: "",
                                            name: "",
                                            btnName: "详情"
                                        },
                                    ];
                                    return _.template($("#temp_op").html(), {
                                        sign: "op_video",
                                        id: obj.aData.id,
                                        name: obj.aData.dispatchBusinessId,
                                        ops: ops
                                    });
                                }
                            },
                        ],

                        fnServerParams: function (aoData) {
                            aoData = $.merge(aoData, that.getQueryParams());

                        },
                        fnDrawCallback: function () {
                            var column = $("#form_live_search input[name=search_videoType]").val();
                            if (column == 1) {
                                $("#table_live_list").find("tr").find("td:eq(5)").hide()
                                $("#table_live_list th:eq(5)").hide()
                            }

                        }
                    }
                );
            },
            refreshTable: function () {
                if (this.dataTable) {
                    this.dataTable.fnDraw();
                } else {
                    this.initTable();
                }
            },
            dispatchBusinessId: function (opts) {
                var that = this;
                $.ajax({
                    url: that.options.host + "/video/dispatchBusinessId",
                    type: "post",
                    data: {id: opts.id, name: opts.name},
                    success: function (data) {
                        if (data.rsCode == "A00000") {
                            $.gritter.add({title: "提示信息：", text: "操作成功！", time: 1000});
                            $("#cancel")[0].click()
                            that.refreshTable()
                        } else {
                            $.gritter.add({title: "提示信息：", text: data.rsMsg, time: 2000});
                        }
                    }
                });
            },
            liveDetails:function (opts) {
                var that = this;
                $("div[data-sign=content]").hide();
                $("#content_live_details").show();
                var url = this.options.host + "/video/live/details";
                var params = {id: opts.id};
                $("#div_live_details").empty();
                $("#div_live_details").block({message: "Loading..."});
                $("#div_live_details").load(url + " #form_live_details", params, function (responseText, textStatus, XMLHttpRequest) {
                    $("#div_live_details").unblock();
                });
            },
        };
        return Live;
    }
)
;